<template>
  <div>
    <!-- :model标签开启双向数据绑定,:rules标签开启表单验证，ref指定表单名-->
    <el-form :model="templateForm" label-width="80px" :rules="templateFormRules" ref="templateForm">
      <el-form-item label="所属站点" prop="siteId">
        <el-select v-model="templateForm.siteId" placeholder="请选择站点">
          <el-option
            v-for="item in siteList"
            :key="item.siteId"
            :label="item.siteName"
            :value="item.siteId">
          </el-option>
        </el-select>
      </el-form-item>

      <el-form-item label="模板名称" prop="templateName">
        <el-input v-model="templateForm.templateName" auto-complete="off"></el-input>
      </el-form-item>
      <el-form-item label="模板参数" prop="templateParameter">
        <el-input v-model="templateForm.templateParameter" auto-complete="off"></el-input>
      </el-form-item>
      <el-form-item label="模板文件路径" prop="templateFilePath">
        <el-input v-model="templateForm.templateFilePath" auto-complete="off"></el-input>
      </el-form-item>
      </el-form-item>
      <el-form-item label="创建时间">
        <el-date-picker type="datetime" placeholder="创建时间" v-model="templateForm.templateCreateTime">
        </el-date-picker>
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button type="primary" @click="go_back">返回</el-button>
      <el-button type="primary" @click="editSubmit">提交</el-button>
    </div>
  </div>
</template>
<script>
    //导入cmsApi中的所有接口
    import * as cmsApi from '../api/cms'

    export default {
        data() {
            return {
                //页面模板Id
                templateId: '',
                //站点列表数据
                siteList: [],
                //新增模板界面表单数据
                templateForm: {
                    siteId: '',
                    templateName: '',
                    templateParameter: '',
                    templateFilePath: '',
                    templateCreateTime: new Date(),
                },
                //表单校验条件，校验提示信息
                templateFormRules: {
                    siteId: [
                        {required: true, message: '请选择站点', trigger: 'blur'}
                    ],
                    templateName: [
                        {required: true, message: '请输入模板名称', trigger: 'blur'}
                    ],
                    // templateParameter: [
                    //     {required: true, message: '请输入模板参数', trigger: 'blur'}
                    // ],
                    templateFilePath: [
                        {required: true, message: '请输入模板文件路径', trigger: 'blur'}
                    ]
                }
            }
        },
        methods: {
            //返回主页面调用的方法
            go_back() {
                //设置路由
                this.$router.push({
                    path: '/cms/template/list',
                    //设置路由携带的参数为当前路由中的参数
                    query: {
                        page: this.$route.query.page,
                        siteId: this.$route.query.siteId
                    }
                })
            },
            editSubmit() {
                this.$refs.templateForm.validate((valid) => {
                    if (valid) {
                        this.$confirm('确认提交吗？', '提示', {}).then(() => {
                            this.addLoading = true;
                            cmsApi.template_edit(this.templateId, this.templateForm).then((res) => {
                                console.log(res);
                                if (res.success) {
                                    this.addLoading = false;
                                    this.$message({
                                        message: '提交成功',
                                        type: 'success'
                                    });
                                    //返回主页面
                                    this.go_back();
                                } else {
                                    this.addLoading = false;
                                    this.$message.error('提交失败');
                                }
                            });
                        });
                    }
                });
            },
            //查询站点列表
            querySiteList: function () {
                cmsApi.site_list().then((res) => {
                    this.siteList = res.queryResult.list;
                })
            }
        },
        //钩子方法，页面创建渲染之前调用
        created: function () {
            //从url中获取templateId,params指的是从url中获取指定参数
            this.templateId = this.$route.params.templateId;
            //根据主键查询页面信息
            cmsApi.template_get(this.templateId).then((res) => {
                console.log(res);
                if (res) {
                    //填充表单数据
                    this.templateForm = res;
                }
            });
        },
        //钩子方法，在页面渲染之后调用
        mounted: function () {
            //加载下拉框数据
            this.querySiteList();
        }
    }
</script>
<style>

</style>
